//保存insert
$("#save").click(function (event) {

	if ($("#educationType").val() < 1) {
		alert('请选择学历类别!');
		return;
	}
	var people = {
		tid: $("#tname").find("option:selected").attr("value"),
		educationType: $("#educationType").find("option:selected").attr("value"),
		cengci: $("#cengci").find("option:selected").attr("value"),
		school: $("input[name='school']").val(),
		major: $("input[name='major']").val(),
		name: $("input[name='name']").val(),
		phone: $("input[name='phone']").val(),
		cardid: $("input[name='cardid']").val(),
		mail: $("input[name='mail']").val(),
		sosperson: $("input[name='sosperson']").val(),
		sosphone: $("input[name='sosphone']").val(),
		remarks: $("input[name='remarks']").val(),
		money: $("input[name='money']").val(),
		oweafee: $("input[name='oweafee']").val(),
		chengben: $("input[name='chengben']").val(),
		//统考课 论文 学士学位
		unifiedExam: $('#unifiedExam').prop('checked') ? 1 : 0,
		paper: $('#paper').prop('checked') ? 1 : 0,
		baccalaureate: $('#baccalaureate').prop('checked') ? 1 : 0
	};
	if ($("input[name='addorupdate']").val() == 0) { //添加
		people.ispay = 0;
		people.creattime = getTime();
		$.ajax({
			url: bulicUrl + "addInfo.php",
			data: people,
			type: "POST",
			dataType: "text",
			success: function (data) {
				if (data == "1") {
					alert("添加成功！");
					addStudentInfo();
					$("#selectbyCondition").click();
				} else {
					alert("添加失败！");
				}
			}
		})
	} else { //修改
		if ($("input[name='money']").attr("disabled") == "disabled") {
			people.ispay = 1;
		}
		people.creattime;
		people.id = $("input[name='id']").val();

		$.ajax({
			url: bulicUrl + "updated.php",
			data: people,
			type: "POST",
			dataType: "text",
			success: function (data) {
				if (data == "1") {
					alert("修改成功！");
					/*$("#selectbyCondition").click();*/
					$("#selectbyCondition").click();
				} else {
					alert("修改失败！");
				}
			}
		})
	}
})

//确认返费isRefund

function isRefund(id, checkQuery) {
	if (window.sessionStorage["scale"] != "1") {
		return;
	}
	$.ajax({
		url: bulicUrl + "updateIsFanMoney.php",
		data: {
			id: id,
			num: 1,
			checkQuery: checkQuery
		},
		type: "POST",
		dataType: "text",
		success: function (data) {
			if (data != "0") {
				alert('返费成功');
				$("#selectbyCondition").click();
			} else {
				alert("返费失败!");
			}
		}
	})
}

//更改num    1是已报名  2是确认缴费  3取消报名   4取消缴费  5 删除  *
//批量根据sole 1 为一个 2 为批量
function isUpdata(id, num, checkQuery) {

	$.ajax({
		url: bulicUrl + "checkisPay.php",
		data: {
			id: id,
			num: num,
			checkQuery: checkQuery
		},
		type: "POST",
		dataType: "text",
		success: function (data) {
			if (data != "0") {
				alert('操作成功');
				$("#selectbyCondition").click();
			} else {
				alert("操作失败!");
			}
		}


	})
}



/*确认返费*/
function updatedisfun(event) {
	if (window.sessionStorage["scale"] != "1") {
		return;
	}
	//拿到对应id
	// var name = $(event).attr("name"); //拿到对应id
	// var money = ;//拿金额
	//if (confirm('是否为学号为: ' + $(event).attr("id") + ' 的学生信息添加返费?')) {
	$.ajax({
		url: bulicUrl + "updateIsFanMoney.php",
		data: {
			id: $(event).attr("id"),
			Refund: $(event).prev().val(),
			num: 2
		},
		type: "POST",
		dataType: "text",
		success: function (data) {
			if (data == 1) {
				alert("添加成功!");
				$("#selectbyCondition").click();
			} else {
				alert("添加失败!");
			}
		}
	})
	//}
}

//添加
function addStudentInfo() {
	//超级管理元显示成本
	if (window.sessionStorage["scale"] == 4 || window.sessionStorage["scale"] == 1) {
		/*查询老师添加到添加学生页面*/
		selecTeachers()
		$('#tname').attr("disabled", false);
		if (window.sessionStorage["scale"] == 1) {
			$("#chengben").attr("style", "display:block");
			$('#tname').attr("disabled", false);
		}
	} else {
		$('#tname').attr("disabled", true);
	}

	//清空内容
	setVal("addorupdate", "0");
	setVal("name", "");
	setVal("mail", "");
	setVal("phone", "");
	setVal("cardid", "");
	setVal("sosperson", "");
	setVal("sosphone", "");
	setVal("school", "");
	setVal("major", "");
	setVal("money", "0");
	setVal("remarks", "");
	setVal("oweafee", "0");
	setVal("chengben", "0");
	$("#educationType").val("0");
	$("#cengci").val("0");
	$("input[name='money']").removeAttr("disabled");
	$("#tname option:selected").text(window.sessionStorage["userName"]);
	$("#tname option:selected").val(window.sessionStorage["userId"]);
}

/*编辑设置*/
function setDatal(data) {

	if (window.sessionStorage["scale"] == 1 || window.sessionStorage["scale"] == 4) {
		$('#tname').removeAttr("disabled");
	}
	if (window.sessionStorage["scale"] == 1) {
		$("#chengben").attr("style", "display:block");
		//$('#chengben').removeAttr("display");
	}

	$.ajax({
		url: bulicUrl + "tName.php",
		type: "POST",
		dataType: "text",
		success: function (dat) {
			dta = JSON.parse(dat);
			$("#tname option:selected").remove();
			var count = 0;
			dta.forEach(function (value, index) {
				$("<option value=" + value.id + ">" + value.tname + "</option>").appendTo("#tname");
				count++;
				if (count == dta.length) { //所有名字追加到下拉框中后执行
					$.ajax({
						url: bulicUrl + "selectOneClassify.php",
						data: {
							id: data.id,
							classify: "students"
						},
						type: "POST",
						dataType: "text",
						success: function (da) {
							dta = JSON.parse(da)[0];
							setVal("id", dta.id);
							setVal("name", dta.name);
							setVal("phone", dta.phone);
							setVal("cardid", dta.cardid);
							setVal("mail", dta.mail);
							setVal("sosperson", dta.sosperson);
							setVal("sosphone", dta.sosphone);
							setVal("school", dta.school);
							setVal("major", dta.major);
							setVal("remarks", dta.remarks);
							setVal("money", dta.money);
							setVal("oweafee", dta.oweafee);
							setVal("chengben", dta.chengben);
							$("#educationType").val(dta.educationType);
							$("#cengci").val(dta.cengci);
							// setVal("tname", dta.tname);
							// setVal("tname", 123);
							$("#tname option:selected").text(dta.tname);
							$("#tname option:selected").val(dta.tid);
							/*是否支付*/
							if (dta.ispay == 1) {
								if (window.sessionStorage["scale"] == 1) {
									$("input[name='money']").removeAttr("disabled");
								} else {
									$("input[name='money']").attr("disabled", "disabled");
								}

							} else {
								$("input[name='money']").removeAttr("disabled");
							}
							//统考课 论文
							dta.unifiedExam == 0 ? $('#unifiedExam').prop('checked', false) : $('#unifiedExam').prop('checked', true)
							dta.paper == 0 ? $('#paper').prop('checked', false) : $('#paper').prop('checked', true)
							dta.baccalaureate == 0 ? $('#baccalaureate').prop('checked', false) : $('#baccalaureate').prop('checked', true)
						}
					})
				}
			})
		}
	})
	// $("#rid").val(data.rid);
	// $("#type").val(data.type);
}
/*查询老师添加到添加学生页面*/
function selecTeachers() {
	$.ajax({
		url: bulicUrl + "tName.php",
		type: "POST",
		dataType: "text",
		success: function (dat) {
			dta = JSON.parse(dat);
			$("#tname option:selected").remove();
			var count = 0;
			dta.forEach(function (value, index) {
				$("<option value=" + value.id + ">" + value.tname + "</option>").appendTo("#tname");
				count++;
			})
		}
	})
}

//打开发票视图
function openBillView(data) {

	window.sessionStorage.removeItem("name");
	window.sessionStorage.removeItem("tname");
	window.sessionStorage.removeItem("educationType");
	window.sessionStorage.removeItem("money");
	window.sessionStorage.setItem('name', data.name); //学生姓名
	window.sessionStorage.setItem('tname', data.tname); //报名老师
	window.sessionStorage.setItem('educationType', mapWrite(data.educationType)); //学历类型
	window.sessionStorage.setItem('money', toChinesNum(data.money)); //缴费金额

	window.open("bill.html", '打印发票', 'width=' + 800 + ',height=' + 600 + ',left=' + (window.screen.width - 800) / 2 + ',top=' + (window.screen.height - 600) / 2 + ',resizable=no,status=yes,toolbar=no,location=no,menubar=no,menu=yes,scrollbars=yes');
}

//完成将 toChineseNum， 可以将数字转换成中文大写的表示，处理到万级别，例如 toChineseNum(12345)，返回 一万二千三百四十五。
function toChinesNum(num) {
	let changeNum = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']; //changeNum[0] = "零"
	let unit = ["", "拾", "佰", "仟", "万"];
	num = parseInt(num);
	let getWan = (temp) => {
		let strArr = temp.toString().split("").reverse();
		let newNum = "";
		for (var i = 0; i < strArr.length; i++) {
			newNum = (i == 0 && strArr[i] == 0 ? "" : (i > 0 && strArr[i] == 0 && strArr[i - 1] == 0 ? "" : changeNum[strArr[i]] + (strArr[i] == 0 ? unit[0] : unit[i]))) + newNum;
		}
		return newNum;
	}
	let overWan = Math.floor(num / 10000);
	let noWan = num % 10000;
	if (noWan.toString().length < 4) noWan = "0" + noWan;
	return overWan ? getWan(overWan) + "万" + getWan(noWan) + '元整' : getWan(num) + '元整';

}
